/****************************************************************************************************************************
 * @File name:result.c
 * @Author:sunshu
 * @Version:v1.00
 * @Date:2018/01/20
 * @Description:
 * @Others:
 * @Function List:
 * @History:
****************************************************************************************************************************/

#include "stm32f4xx_hal.h"
#include "result.h"
#include "short_test.h"
#include "led.h"
#include "w25qxx.h"
#include "command.h"

/****************************************************************************************************************************
* @Functions:ReadShortResult()
* @Description:read short result from DisplayData-type data
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char ReadShortResult(DisplayData *pDispData)
{
    unsigned char* pdata;
    unsigned char i;
    unsigned char shortcount;
    unsigned char ledstate;

    shortcount = 0;
    ledstate = TEST_OK;
    pdata = (unsigned char*)pDispData;
    for(i=0; i<sizeof(DisplayData); i++)
    {
        if(pdata[i] != STATUS_OK)
        {
            shortcount++;
        }
    }
    if(shortcount > 1)
    {
        ledstate = TEST_FAIL;
    }
    return ledstate;
}

/****************************************************************************************************************************
* @Functions:ReadOpenResult()
* @Description:read open result from DisplayData-type data
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char ReadOpenResult(DisplayData *pDispData)
{
    unsigned char* pdata;
    unsigned char i;
    unsigned char opencount;
    unsigned char ledstate;

    opencount = 0;
    ledstate = TEST_OK;
    pdata = (unsigned char*)pDispData;
    for(i=0; i<sizeof(DisplayData); i++)
    {
        if(pdata[i] != STATUS_OK)
        {
            opencount++;
        }
    }

    if(opencount != 0)
    {
        if(opencount != 3)
        {
            ledstate = TEST_FAIL;
        }
        else
        {
            if(pDispData->lcd_b05 != STATUS_OPEN)
            {
                ledstate = TEST_FAIL;
            }
            if(pDispData->lcd_b06 != STATUS_OPEN)
            {
                ledstate = TEST_FAIL;
            }
            if(pDispData->lcd_b07 != STATUS_OPEN)
            {
                ledstate = TEST_FAIL;
            }
        }
    }
    return ledstate;
}

/****************************************************************************************************************************
* @Functions:GetAdjItemResult()
* @Description:get adj-item result
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char GetAdjItemResult(void)
{
    unsigned int i;
    unsigned char errcount;
    float Value = 0;

    uint32_t GrpFlag;

    errcount = 0;

    GrpFlag = Readu32DatFrom_FlashMCUReg(EEP_OFFSET_GRP);

    if((GrpFlag & (1<<0)) == 0)
    {
        return TEST_OK;
    }
    for(i=0; i<ADJITEM_NUM; i++)
    {
        Value = AdjItem[i].Value;
        if(AdjItem[i].Type != SKIP)
        {
//			if(Value < AdjItem[i].ShortLim || Value > AdjItem[i].OpenLim)
//			{
//				errcount++;
//			}

            if(AdjItem[i].Type != CAP)
            {
                {
                    if((Value < (uint32_t)AdjItem[i].ShortLim) || (Value > (uint32_t)AdjItem[i].OpenLim))
                    {
                        errcount++;
                    }
                }
            }
            else
            {
                if(AdjItem[i].Value>0)
                {
                    if((Value > (uint32_t)AdjItem[i].ShortLim) || (Value < (uint32_t)AdjItem[i].OpenLim))
                    {
                        errcount++;
                    }
                }
                else
                {
                    errcount++;
                }
            }

        }
    }
    if(errcount > 0)
    {
        return TEST_FAIL;
    }
    return TEST_OK;
}

/****************************************************************************************************************************
* @Functions:GetGndItemResult()
* @Description:get gnd-item result
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char GetGndItemResult(void)
{
    unsigned char i;
    unsigned char errcount;
    uint32_t GrpFlag;
    float Value=0;
    errcount = 0;

    GrpFlag = Readu32DatFrom_FlashMCUReg(EEP_OFFSET_GRP);

    if((GrpFlag & (1<<1)) == 0)
    {
        return TEST_OK;
    }

    for(i=0; i<GNDITEM_NUM; i++)
    {
        Value = GndItem[i].Value;
        if(GndItem[i].Type != SKIP)
        {
            if((GndItem[i].Type != CAP) && (GndItem[i].Type != CAP2))
            {
                if((Value < (uint32_t)GndItem[i].ShortLim) || (Value > (uint32_t)GndItem[i].OpenLim))
                {
                    errcount++;
                }
            }
            else
            {
                if(GndItem[i].Value>0)
                {
                    if((Value > (uint32_t)GndItem[i].ShortLim) || (Value < (uint32_t)GndItem[i].OpenLim))
                    {
                        errcount++;
                    }
                }
                else
                {
                    errcount++;
                }
            }
        }
    }
    if(errcount > 0)
    {
        return TEST_FAIL;
    }
    return TEST_OK;
}

/****************************************************************************************************************************
* @Functions:GetVbusItemResult()
* @Description:get vbus-item result
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char GetVbusItemResult(void)
{
    unsigned char i;
    unsigned char errcount;
    uint32_t GrpFlag;
    float Value=0;

    errcount = 0;

    GrpFlag = Readu32DatFrom_FlashMCUReg(EEP_OFFSET_GRP);

    if((GrpFlag & (1<<2)) == 0)
    {
        return TEST_OK;
    }

    for(i=0; i<VBUSITEM_NUM; i++)
    {
        Value = VbusItem[i].Value;
        if(VbusItem[i].Type != SKIP)
        {
//			if(Value < VbusItem[i].ShortLim || Value > VbusItem[i].OpenLim)
//			{
//				errcount++;
//			}

            if(VbusItem[i].Type != CAP)
            {
                {
                    if((Value < (uint32_t)VbusItem[i].ShortLim) || (Value > (uint32_t)VbusItem[i].OpenLim))
                    {
                        errcount++;
                    }
                }
            }
            else
            {
                if(VbusItem[i].Value>0)
                {
                    if((Value > (uint32_t)VbusItem[i].ShortLim) || (Value < (uint32_t)VbusItem[i].OpenLim))
                    {
                        errcount++;
                    }
                }
                else
                {
                    errcount++;
                }
            }
        }
    }
    if(errcount > 0)
    {
        return TEST_FAIL;
    }
    return TEST_OK;
}

/****************************************************************************************************************************
* @Functions:GetTRItemResult()
* @Description:get TR-item result
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char GetTRItemResult(void)
{
    unsigned char i;
    unsigned char errcount;
    uint32_t GrpFlag;
    errcount = 0;
    uint8_t	 gnd_item_adr[8]= {0,1,8,9,10,11,18,19};
    uint8_t	 is_tr;

    GrpFlag = Readu32DatFrom_FlashMCUReg(EEP_OFFSET_GRP);

    if((GrpFlag & (3<<3)) == 0 || (GrpFlag & (1<<1)) == 0)
    {
        return TEST_OK;
    }

    if((GrpFlag & (3<<3)) == 0)											//TR and dds off
    {
        is_tr = 0;
    }
    else if((GrpFlag & (3<<3)) == 0x00000010)				//dds on
    {
        is_tr = 2;
    }
    else																						//TR on
    {
        is_tr = 1;
    }

    for(i=0; i<8; i++)
    {
        if(GndItem[gnd_item_adr[i]].Type != SKIP)
        {
            if(is_tr == 1)
            {
                if(TRItem[i].Value > (uint32_t)TRItem[i].ShortLim)
                {
                    errcount++;
                }
            }
            else if(is_tr == 2)
            {
                if((TRItem[i].Value > (uint32_t)TRItem[i].OpenLim) || (TRItem[i].Value < (uint32_t)TRItem[i].ShortLim ))
                {
                    errcount++;
                }
            }
        }
    }

    if(errcount > 0)
    {
        return TEST_FAIL;
    }
    return TEST_OK;
}


unsigned char GetVOH_ItemResult(void)
{
    unsigned char i;
    unsigned char errcount;
    uint32_t GrpFlag;
    uint8_t	 gnd_item_adr[2]= {3,13};
    errcount = 0;

    GrpFlag = Readu32DatFrom_FlashMCUReg(EEP_OFFSET_GRP);

    if((GrpFlag & (1<<5)) == 0)											//VOH off
    {
        return TEST_OK;
    }

    for(i=0; i<2; i++)																//a05,b05
    {
        if(GndItem[gnd_item_adr[i]].Type != SKIP)
        {
            if(VOHItem[i].Value < (uint32_t)VOHItem[i].OpenLim)
            {
                errcount++;
            }
        }
    }


    if(errcount > 0)
    {
        return TEST_FAIL;
    }

    return TEST_OK;
}



unsigned char GetVOL_ItemResult(void)
{
    unsigned char i;
    unsigned char errcount;
    uint32_t GrpFlag;
    uint8_t	 gnd_item_adr[2]= {3,13};
    errcount = 0;

    GrpFlag = Readu32DatFrom_FlashMCUReg(EEP_OFFSET_GRP);

    if((GrpFlag & (1<<6)) == 0)											//VOL off
    {
        return TEST_OK;
    }

    for(i=0; i<2; i++)																//a05,b05
    {
        if(GndItem[gnd_item_adr[i]].Type != SKIP)
        {
            if(VOLItem[i].Value > (uint32_t)VOLItem[i].ShortLim)
            {
                errcount++;
            }
        }
    }


    if(errcount > 0)
    {
        return TEST_FAIL;
    }

    return TEST_OK;
}

unsigned char GetVBUS_DDS2_ItemResult(void)
{
    unsigned char errcount;
    uint32_t GrpFlag;
    errcount = 0;

    GrpFlag = Readu32DatFrom_FlashMCUReg(EEP_OFFSET_GRP);

    if((GrpFlag & (1<<7)) == 0)											//VBUS_DDS2 off
    {
        return TEST_OK;
    }


    if(GndItem[2].Type != SKIP)										//a04
    {
        if(DDS2Item[0].Value < (uint32_t)DDS2Item[0].ShortLim)
        {
            errcount++;
        }
    }


    if(errcount > 0)
    {
        return TEST_FAIL;
    }

    return TEST_OK;
}

/****************************************************************************************************************************
* @Functions:LedDispResult()
* @Description:flush led state
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char LedDispResult(void)
{
    unsigned char ledstate;

    if(DispMode == UPDATE_MODE_NC)
    {
        ledstate = TEST_FAIL;
    }
    else
    {
        if(GetAdjItemResult() == TEST_FAIL || GetGndItemResult() == TEST_FAIL || GetVbusItemResult() == TEST_FAIL || GetTRItemResult() == TEST_FAIL || GetVOH_ItemResult() == TEST_FAIL \
                || GetVOL_ItemResult() == TEST_FAIL || GetVBUS_DDS2_ItemResult() == TEST_FAIL)
        {
            ledstate = TEST_FAIL;
        }
        else
        {
            ledstate = TEST_OK;
        }
    }
    return ledstate;
}





